﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;

namespace MongodbDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //安装驱动包
            //Install-Package mongocsharpdriver
            var client = new MongoClient("mongodb://localhost:27017");
            var database = client.GetDatabase("foo");
            var collection = database.GetCollection<BsonDocument>("bar");

            collection.InsertOneAsync(new BsonDocument("Name", "Jack")).Wait();

            var list = collection.Find(new BsonDocument("Name", "Jack"))
                .ToListAsync().Result;

            foreach (var document in list)
            {
                Console.WriteLine(document["Name"] + ",id="+document["_id"]);
            }
            Console.Read();
        }
    }
}
